<!DOCTYPE html>
<html>
  <head th:include="/common/head :: head"></head>
  <style>
		.check_box {
				width:20px;
				height:20px
		}
  </style>
  <body class="layui-anim layui-anim-up">
    <div class="x-nav">
      <span class="layui-breadcrumb">
        <a href="">首页</a>
        <a href="">销售管理</a>
        <a><cite>销售绩效</cite></a>
      </span>
      <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="iconfont" style="line-height:30px">&#xe6aa;</i></a>
    </div>
    <div class="x-body" ng-app="listApp" ng-controller="listController">
	  <div class="layui-row" style="text-align:center">
	  <form class="layui-form layui-col-md12 x-so">
	      <div class="layui-inline">
	      	  <label>姓名&nbsp;:</label>
		      <div class="layui-input-inline">
					<select  id="userId" name="userId" lay-search="">
						<option value=""></option>
					</select>
			  </div>
	      </div>
     
	      <button class="layui-btn btn_search" lay-submit="" lay-filter="sreach">
						<i class="layui-icon">&#xe615;</i>
		  </button>
	
	 </form>
	  </div>
	      <xblock>
	        <button class="layui-btn" onclick="addMent('添加员工绩效','/achievement/mentAdd.shtml',455,400)"><i class="layui-icon"></i>添加</button>
	        <button class="layui-btn layui-btn-danger" onclick="del()">                      <i class="layui-icon"></i>删除</button>
	        <button class="layui-btn" onclick="ment('查看详情','/achievement/mentQuery.shtml',455,450)"><i class="iconfont">&#xe806;</i>查看</button>
	        <button class="layui-btn" onclick="ment('编辑绩效','/achievement/edit.shtml',455,450)"><i class="layui-icon">&#xe642;</i>修改</button>
	        <span class="x-right" style="line-height:40px" id="span_total"></span>
	      </xblock>
	      <table class="layui-table">
	        <thead>
	          <tr>
	          	<th></th>
	            <th>ID</th>
	            <th>姓名</th>
	            <th>绩效等级</th>
	            <th>最后修改时间</th>
	            <th>操作人</th>
	        </thead>
	        <tbody>
	          <tr ng-repeat="ment in achievementList">
	          	<td>
	              <input type="checkbox" name="checkedMent" class="check_box childs_check" value="{{ment.id}}"/>
	            </td>
	            <td>{{ment.id}}</td>
	            <td>{{ment.userId}}</td>
	            <td>
	            	<span ng-if="ment.level>=0">+{{ment.level}}</span>
	            	<span ng-if="ment.level<0">{{ment.level}}</span>
	            </td>
	            <td>{{ment.updateDate}}</td>
	            <td>{{ment.createBy}}</td>
	          </tr>
	        </tbody>
	      </table>		
		<div id="page"></div>
	</div>
  </body>
	<script type="text/javascript">
		//全局对象
		var _this={"current":1,"size":5}
		angular.module("listApp",[]).controller("listController",function($scope){
			  _this.scope = $scope; 
		  })
		
		$(function(){
			//获取数据
			getData();
			//获取用户姓名、id列表
			getUserId();
			//绑定事件
			bindEvent();
		})
		//条件查询
		function bindEvent(){
			$(".btn_search").bind("click",function(){
				_this.userId=$("#userId").val();
				_this.current=1;
				getData();
			})
		}
		//获取数据
		function getData(){
			$.ajax({
				url:"/achievement/getAchievementList",
				type:"post",
				data:{
					"page":_this.current,
					"size":_this.size,
					"userId":_this.userId
				},
				success:function(result){
					if(result,status==0){
						if(result.data.pages!=0 && result.data.records==0){
							_this.current=result.data.pages;
							getData();
							return;
						}
						showData(result);
					}else{
						layer.msg(result.msg);
					}
				}
			})
		}
		//展示数据
		function showData(result){
			//展示
			layui.use(['laypage', 'layer'], function(){
				 laypage = layui.laypage;
				 laypage.render({   //设置基础参数
				  elem: 'page',        //指向存放分页的容器，值可以是容器ID、DOM对象
				  count: result.data.total,   //数据总数
				  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
				  limits: [5,10, 20],    //每页条数的选择项
				  curr:_this.current,    //起始页
				  limit:_this.size,      //每页显示的条数
				  jump: function(obj,first){  //jump切换分页的回调
					  				//obj（当前分页的所有选项值）、first（是否首次，一般用于初始加载的判断）
					  if(!first){
						  _this.current = obj.curr;
						  _this.size = obj.limit;
						  getData();
					  }
				  }
				 });
			})
			//展示列表
			_this.scope.achievementList = result.data.records;
			_this.scope.$apply();
			$("#span_total").html("共有数据:"+result.data.total+"条");
		}
		//异步改为同步  async:false;
		//获取用户姓名、id列表
		function getUserId(){
			$.ajax({
				url:"/user/getUserIds",
				type:"post",
				async:false,
				success:function(result){
					if(result.status==0){
						showUserName(result);	
					}else{
						layer.msg(result.msg);
					}
				}	
			})
		}
		//展示下拉框
		function showUserName(result){
			var str= "";
			var list = result.data;
			for(i in list){
				str+="<option  value="+list[i].id+">"+list[i].userName+"</option>";
			}
			$("#userId").append(str);
		}
		//添加按钮
		function addMent(title,url,w,h){
			parent.layer.open({
				type:2,
				area:[w+'px',h+'px'],
				fix:false,
				maxmin:true,
				shadeClose:true,
				shade:0.4,
				title:title,
				content:url,
				end:function(){
					getData();
				}
			})
		}
		//删除按钮
		function del(){
			var mentIds = "";
			//拿到指定名称对象的集合
			var mentIDs = document.getElementsByName("checkedMent");
			for( i in mentIDs){
				if(mentIDs[i].checked==true){
					mentIds +=mentIDs[i].value+","
				}
			}
			if(mentIds==""){
				parent.layer.msg("至少选择一条数据，做删除");
				return;
			}
			mentIds=mentIds.substr(0,mentIds.length-1);
			layer.confirm("确定删除吗？",function(index){
				//删除
				deleteMents(mentIds);
			})
		}
		//删除操作
		function deleteMents(ids){
			$.post("/achievement/deleteMent","ids="+ids,function(result){
				if(result.status==0){
					layer.msg(result.msg,function(){
						icon:1;
					})
					getData();
				}else{
					layer.msg(result.msg,function(){
						icon:2;
					});
				}
			})
		}
		//查看 、修改按钮
		function ment(title,url,w,h){
			var ids=0;
			var id="";
			var queryIds =document.getElementsByName("checkedMent");
			for( i in queryIds){
				if(queryIds[i].checked==true){
					ids+=1;
					if(ids>1){
						parent.layer.msg("最多查看一条记录");
						return ;
					}
					id = queryIds[i].value;
				}
			}
			if(id==""){
				parent.layer.msg("必须选中一条记录");
				return ;
			}
			sessionStorage.setItem("mentId",id);
			parent.layer.open({
				type: 2,
		        area: [w+'px', h +'px'],
		        fix: false, //不固定
		        maxmin: true,
		        shadeClose: true,
		        shade:0.4,
		        title: title,
		        content: url,
		        end:function(){
		        	getData();
		        }
			})
		}
		
	</script>
</html>